package com.neutech.maplestory.entity;
import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;


import java.awt.*;
import java.util.Random;

public class Mob extends AbstractMapleStoryObject{
    public static Image[] imgs = new Image[100];
    static {
        for (int i = 0; i < 2; i++) {
        imgs[i] = ImageUtil.getImage("mob01_left_stand"+ i);
     }
        for (int i = 2; i < 5; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_move"+ (i-2));
        }
        for (int i = 5; i < 8; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_die"+ (i-5));
        }
        for (int i = 8; i < 10; i++) {
            imgs[i] = ImageUtil.getImage("mob01_right_stand"+ (i-8));
        }
        for (int i = 10; i < 13; i++) {
            imgs[i] = ImageUtil.getImage("mob01_right_move"+ (i-10));
        }
        for (int i = 13; i < 16; i++) {
            imgs[i] = ImageUtil.getImage("mob01_right_die"+ (i-13));
        }
    }

    /**
     * 最大血量
     */
    public  int  MAX_hp ;
    public Mob(){
        this.width = imgs[0].getWidth(null);
        this.height = imgs[0].getHeight(null);
        this.dir = Direction.LEFT;
        this.action = Action.WALK;
        this.live = true;
        this.MAX_hp  = 10000;
        this.HP = MAX_hp;
    }



    public Mob(MapleStoryClient msc,int x,int y,int speed){
        this();
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.speed = speed;

    }
    private int count;
    private int step;
    private int dieStep;
    @Override
    public void draw(Graphics g) {
        if(dieStep >= 2){
            this.live = false;
            dieStep = 0;
        }
        if(!live){
            msc.mobs.remove(this);
            return;
        }
        switch (dir){
            case LEFT:
                switch (action) {
                    case STAND:
                        if (count++ % 5 == 0) {
                            g.drawImage(imgs[step++ % 2], x, y, null);
                        } else {
                            g.drawImage(imgs[step % 2], x, y, null);
                        }
                        break;
                    case WALK:
                        if (count++ % 10 == 0) {
                            g.drawImage(imgs[step++ % 3 +2], x, y, null);
                        } else {
                            g.drawImage(imgs[step % 3 +2], x, y, null);
                        }
                        break;
                    case DIE:
                        if (count++ % 10 == 0) {
                            g.drawImage(imgs[dieStep++ % 3 +5], x, y, null);
                        } else {
                            g.drawImage(imgs[dieStep % 3 +5], x, y, null);
                        }
                        break;
                    default:
                        break;
                }
                break;
            case RIGHT:
                switch (action) {
                    case STAND:
                        if (count++ % 5 == 0) {
                            g.drawImage(imgs[step++ % 2+8], x, y, null);
                        } else {
                            g.drawImage(imgs[step %2 +8], x, y, null);
                        }
                        break;
                    case WALK:
                        if (count++ % 10 == 0) {
                            g.drawImage(imgs[step++ % 3 +10], x, y, null);
                        } else {
                            g.drawImage(imgs[step % 3 +10], x, y, null);
                        }
                        break;
                    case DIE:
                        if (count++ % 10 == 0) {
                            g.drawImage(imgs[dieStep++ % 3 +13], x, y, null);
                        } else {
                            g.drawImage(imgs[dieStep % 3 +13], x, y, null);
                        }
                        break;
                    default:
                        break;
                }
            default:
                break;

        }
        if(HP<MAX_hp) {
            bloodBar.draw(g);
        }
        qiqi();
        move();
        outOfBound();


    }
    Random r = new Random();

    /**
     * 怪物的随机状态
     */
    public void qiqi(){
        if (msc.hero.count % 80 == 0 && this.action != Action.DIE){
            int rd = r.nextInt(4);
            switch (rd){
                case 0 :
                    this.dir = Direction.RIGHT;
                    this.action = Action.STAND;
                    break;
                case 1:
                    this.dir = Direction.RIGHT;
                    this.action = Action.WALK;
                    break;
                case 2:
                    this.dir = Direction.LEFT;
                    this.action = Action.STAND;
                    break;
                case 3:
                    this.dir = Direction.LEFT;
                    this.action = Action.WALK;
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void move() {
       switch (dir){
           case LEFT:
               switch (action){
                   case WALK:
                       this.x -= speed;
                       break;
                   default:
                       break;
               }
               break;
           case RIGHT:
               switch (action) {
                   case WALK:
                       this.x += speed;
                       break;
                   default:
                       break;
               }
           default:
               break;
       }
    }


    public BloodBar bloodBar = new BloodBar();
    class BloodBar extends AbstractMapleStoryObject{
    private Image img;
        public BloodBar(){
            this.img = ImageUtil.getImage("blood");
            this.width = img.getWidth(null);
        }
        @Override
        public void draw(Graphics g) {
        /**    //框
            //当内部类访问外部类相同属性名时，使用外部类.this.属性名
            Color c = g.getColor();
            g.drawRect(Mob.this.x,Mob.this.y - 15,Mob.this.width,10);
            g.setColor(Color.cyan);
            g.fillRect(Mob.this.x+1,Mob.this.y - 15+1, (int) (Mob.this.width * Mob.this.HP / Mob.this.MAX_hp),10-1);
            g.setColor(c);
         */
        int n =Mob.this.width/this.width*Mob.this.HP / Mob.this.MAX_hp;
        for (int i=0;i< n;i++){
            g.drawImage(img,Mob.this.x+(i * this.width +1),Mob.this.y - 15,null);
        }
        }
    }
    private  void outOfBound(){
        if(this.y<=300){
            if(this.x <= 750){
                this.x = 750;
                this.dir = Direction.RIGHT;
            }
            if (this.x >= 1355 - this.width){
                this.x = 1355 - this.width;
                this.dir = Direction.LEFT;
            }

        }else
        if(this.x <= 700){
            this.x = 700;
            this.dir = Direction.RIGHT;
        }
        if (this.x >= Constant.GAME_WIDTH - this.width){
            this.x = Constant.GAME_WIDTH - this.width;
            this.dir = Direction.LEFT;
        }
    }

}


